package com.wfk.plat.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wfk.entity.BankType;
import com.wfk.entity.Goods;
import com.wfk.entity.GoodsSpec;
import com.wfk.entity.OrderGoods;
import com.wfk.entity.SysCashDetails;
import com.wfk.entity.SysRule;
import com.wfk.entity.SysService;
import com.wfk.plat.annotation.AuthResource;
import com.wfk.plat.mapping.MappingUtils;
import com.wfk.service.IBankTypeService;
import com.wfk.service.IGoodsService;
import com.wfk.service.IGoodsSpecService;
import com.wfk.service.IOrderGoodsService;
import com.wfk.service.ISysCashDetailsService;
import com.wfk.service.ISysRuleService;
import com.wfk.service.ISysServiceService;
import com.wfk.utils.BasePage;
import com.wfk.utils.ResponseData;

@Controller
public class FinanceController {
	
	@Autowired
	private ISysCashDetailsService cashDetailsService ;
	@Autowired
	private IBankTypeService bankTypeService ;
	@Autowired
	private IOrderGoodsService orderGoodsService;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private IGoodsSpecService goodsSpecService;
	@Autowired
	private ISysServiceService sysServiceService;
	@Autowired
	private ISysRuleService sysRuleService;
	
	/**
	 * 
	 * @param nid
	 * @return
	 */
	@AuthResource(url=MappingUtils.sysRuleList)
	@RequestMapping(value = MappingUtils.sysRuleDev)
	@ResponseBody
	public ResponseData sysRuleDev(Integer id) {
		boolean removeById = sysRuleService.removeById(id);
		return removeById?ResponseData.ok():ResponseData.baseError("删除失败");
	}
	
	/**
	 * 
	 * @param nid
	 * @return
	 */
	@AuthResource(url=MappingUtils.sysRuleList)
	@RequestMapping(value = MappingUtils.sysRuleEdit)
	@ResponseBody
	public ResponseData saveNews(SysRule sysRule) {
		ResponseData result = sysRuleService.saveSysRule(sysRule);
		return result;
	}
	
	@AuthResource(url=MappingUtils.sysRuleList)
	@RequestMapping(MappingUtils.sysRuleAdd)
	public ModelAndView sysRuleAdd(Integer id,Integer loginId) {
		ModelAndView view = new ModelAndView(MappingUtils.sysRuleAdd);
		view.addObject("vo",sysRuleService.getById(id));
		view.addObject("loginId",loginId);
		return view;
	}
	
	@AuthResource()
	@RequestMapping(MappingUtils.sysRuleList)
	public ModelAndView sysRuleList(Integer loginId,SysRule vo,BasePage<SysRule> page) {
		ModelAndView view = new ModelAndView(MappingUtils.sysRuleList);
		this.sysRuleService.getLit(vo, page);
		view.addObject("loginId",loginId);
		view.addObject("list",page);
		return view;
	}
	
//	@AuthResource(url=MappingUtils.goodsList)
	@RequestMapping(MappingUtils.goodsEdit)
	@ResponseBody
	public ResponseData goodsSave(Goods goods,MultipartFile pfile,MultipartFile[] ufile,Integer loginId){
		try{
			return goodsService.insertGoods(goods, pfile, ufile);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseError("内部错误");
		}
	}
	
	@AuthResource(url=MappingUtils.goodsList)
	@RequestMapping(value=MappingUtils.goodsStatus)
	@ResponseBody
	public ResponseData goodsStatus(String goodsCode,Integer status,Integer loginId) {
		Goods goods = goodsService.getById(goodsCode);
		if(goods==null) {
			return ResponseData.badRequest();
		}else if(goods.getStatus()==status) {
			return ResponseData.baseError("商品已经是这个状态");
		}
		goods.setStatus(status);
		this.goodsService.updateById(goods);
		return ResponseData.ok();
	} 
	
	
	@AuthResource(url=MappingUtils.goodsList)
	@RequestMapping(value=MappingUtils.goodsAdd)
	public ModelAndView goodsAdd(String goodsCode,Integer loginId) {
		ModelAndView m = new ModelAndView(MappingUtils.goodsAdd);
		m.addObject("vo", this.goodsService.getById(goodsCode));
		m.addObject("specs",goodsSpecService.list(new QueryWrapper<GoodsSpec>().eq("parent", 0)));
		m.addObject("serviceList",sysServiceService.list(new QueryWrapper<SysService>().eq("parentid", 0)));
		m.addObject("loginId", loginId);
		return m ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.goodsList)
	public ModelAndView orderList(Goods goods,BasePage<Goods> page,Integer loginId) {
		goodsService.getList(goods,page);
		ModelAndView m = new ModelAndView(MappingUtils.goodsList);
		m.addObject("vo", goods);
		m.addObject("loginId", loginId);
		m.addObject("list", page);
		return m ;
	}

	@AuthResource(url=MappingUtils.orderList)
	@ResponseBody
	@RequestMapping(value=MappingUtils.orderStatus)
	public ResponseData orderStatus(Integer orderid,Integer loginId) {
		OrderGoods orderGoods = orderGoodsService.getById(orderid);
		if(orderGoods==null) {
			return ResponseData.badRequest();
		}else if(orderGoods.getStatus()==2) {
			return ResponseData.baseError("订单已经发货");
		}
		orderGoods.setStatus(2);
		this.orderGoodsService.updateById(orderGoods);
		return ResponseData.ok();
	} 
	
	@AuthResource
	@RequestMapping(value=MappingUtils.orderList)
	public ModelAndView orderList(OrderGoods orderGoods,BasePage<OrderGoods> page,Integer loginId) {
		orderGoodsService.getList(page,orderGoods);
		ModelAndView m = new ModelAndView(MappingUtils.orderList);
		m.addObject("vo", orderGoods) ;
		m.addObject("loginId", loginId) ;
		m.addObject("list", page) ;
		return m ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showCashDetailList)
	public ModelAndView showCashDetailList(SysCashDetails cashDetails,BasePage<SysCashDetails> page,Integer loginId) {
		cashDetailsService.getList(cashDetails, page);
		ModelAndView m = new ModelAndView(MappingUtils.showCashDetailList);
		m.addObject("cashDetails", cashDetails) ;
		m.addObject("loginId", loginId) ;
		m.addObject("list", page) ;
		return m ;
	}
	
	@AuthResource(url=MappingUtils.showCashDetailList)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doCashPreper)
	public ResponseData doCashPreper(Integer id,Integer status) {
		return cashDetailsService.doCashPreper(id,status) ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showBankTypeList)
	public ModelAndView showBankTypeList(BankType bankType,BasePage<BankType> page,Integer loginId) {
		bankTypeService.getList(bankType,page) ;
		ModelAndView m = new ModelAndView(MappingUtils.showBankTypeList);
		m.addObject("bankType", bankType) ;
		m.addObject("loginId", loginId) ;
		m.addObject("list", page) ;
		return m ;
	}
	
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showBankTypeAdd)
	public ModelAndView showBankTypeAdd(Integer loginId) {
		ModelAndView m = new ModelAndView(MappingUtils.showBankTypeAdd);
		m.addObject("loginId", loginId) ;
		return m ;
	}
	
	@AuthResource(url=MappingUtils.showBankTypeAdd)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doBankTypeAdd,method= {RequestMethod.POST})
	public ResponseData doBankTypeAdd(BankType bankType) throws IOException {
		return bankTypeService.doBankTypeAdd(bankType) ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showBankTypeEdit)
	public ModelAndView showBankTypeEdit(Integer id,Integer loginId) {
		BankType bankType = bankTypeService.getById(id);
		ModelAndView m = new ModelAndView(MappingUtils.showBankTypeEdit);
		m.addObject("loginId", loginId) ;
		m.addObject("bankType", bankType) ;
		return m ;
	}
	
	@AuthResource(url=MappingUtils.showBankTypeEdit)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doBankTypeEdit)
	public ResponseData doBankTypeEdit(BankType bankType) {
		return bankTypeService.doBankTypeEdit(bankType) ;
	}
}
